﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using Common.Utilities;
using Common.Utilities.Dispatcher;

namespace Workflows.Components.TextMining.EnumerationObjects
{
    /// <summary>
    /// 
    /// </summary>
    [Serializable]
    public class TableLayoutDetectorInstruction:InstructionBase
    {
        #region props
        private Dictionary<int,List<string>> _ColumnHeaderOptions;
        /// <summary></summary>
        public Dictionary<int,List<string>> ColumnHeaderOptions
        {
            get
            {
                return this._ColumnHeaderOptions;
            }
            set
            {
                this._ColumnHeaderOptions = value;
            }
        }

        private string _FileListInputXmlFilePath;
        /// <summary>
        /// <Files><File DocID="0" FilePath="" /></Files>
        /// </summary>
        public string FileListInputXmlFilePath
        {
            get
            {
                return this._FileListInputXmlFilePath;
            }
            set
            {
                this._FileListInputXmlFilePath = value;
            }
        }

        private string _TableScanResultFile;
        /// <summary>
        /// <Files>
        ///     <File DocID="1" Pages="1,2,3" IsElectronic="False" IsScanned="False" />
        /// </Files>
        /// </summary>
        public string TableScanResultFile
        {
            get
            {
                return this._TableScanResultFile;
            }
            set
            {
                this._TableScanResultFile = value;
            }
        }

        private string _TableLayoutOutputFilePath;
        /// <summary>
        /// <Files>
        ///     <File DocID="1">
        ///         <Page PageNumber="10">
        ///             <ColumnHeader Name="Degree" X="4" Y="12" W="12.0" H="12.0"/>
        ///         </Page>
        ///     </File>
        /// </Files>
        /// </summary>
        public string TableLayoutOutputFilePath
        {
            get
            {
                return this._TableLayoutOutputFilePath;
            }
            set
            {
                this._TableLayoutOutputFilePath = value;
            }
        }

        private string _LogFilePath;
        /// <summary></summary>
        public string LogFilePath
        {
            get
            {
                return this._LogFilePath;
            }
            set
            {
                this._LogFilePath = value;
            }
        }

        private string _ErrFilePath;
        /// <summary></summary>
        public string ErrFilePath
        {
            get
            {
                return this._ErrFilePath;
            }
            set
            {
                this._ErrFilePath = value;
            }
        }

        private int _ThreadPoolSize;
        /// <summary></summary>
        public int ThreadPoolSize
        {
            get
            {
                return this._ThreadPoolSize;
            }
            set
            {
                this._ThreadPoolSize = value;
            }
        }
        #endregion

        #region ctor
        /// <summary>
        /// 
        /// </summary>
        public TableLayoutDetectorInstruction():base(Guid.Empty, Guid.Empty)
        {
            this._ColumnHeaderOptions=new Dictionary<int, List<string>>();
            this._FileListInputXmlFilePath = string.Empty;
            this._TableScanResultFile = string.Empty;
            this._TableLayoutOutputFilePath = string.Empty;
            this._LogFilePath = string.Empty;
            this._ErrFilePath = string.Empty;
            this._ThreadPoolSize = 10;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="componentID"></param>
        /// <param name="jobTicketID"></param>
        /// <param name="colHeaderOps"></param>
        /// <param name="fileListXmlFilePath"></param>
        /// <param name="tblScanOutputFilePath"></param>
        /// <param name="tblLayoutOutputFilePath"></param>
        /// <param name="logFilePath"></param>
        /// <param name="errFilePath"></param>
        /// <param name="threadPoolSize"></param>
        public TableLayoutDetectorInstruction(Guid componentID, Guid jobTicketID,
            Dictionary<int,List<string>> colHeaderOps,
            string fileListXmlFilePath,
            string tblScanOutputFilePath,
            string tblLayoutOutputFilePath,
            string logFilePath,
            string errFilePath,
            int threadPoolSize) 
            : base(componentID, jobTicketID)
        {
            this._ColumnHeaderOptions = colHeaderOps;
            this._FileListInputXmlFilePath = fileListXmlFilePath;
            this._TableScanResultFile = tblScanOutputFilePath;
            this._TableLayoutOutputFilePath = tblLayoutOutputFilePath;
            this._LogFilePath = logFilePath;
            this._ErrFilePath = errFilePath;
            this._ThreadPoolSize = threadPoolSize;
        }
        #endregion

        /// <summary>
        /// instantiated from xml
        /// "&lt;Instructions&gt;&lt;Instruction&gt;&lt;/Instruction&gt;&lt;/Instruction&gt;", must
        /// instantiate from first child of root node
        /// </summary>
        public override InstructionBase Instantiate(XmlNode xNode)
        {
            if (xNode.Attributes.GetNamedItem("Type").Value == this.GetType().FullName)
            {
                TableLayoutDetectorInstruction instruction = new TableLayoutDetectorInstruction();
                instruction.OwnerComponentID = new Guid(xNode.Attributes.GetNamedItem("ComponentID").Value);
                instruction.OwnerWorkerInstanceID = new Guid(xNode.Attributes.GetNamedItem("WorkerID").Value);
                instruction.SubmitTime = DateTime.Parse(xNode.Attributes.GetNamedItem("SubmitTime").Value);
                instruction.StartTime = DateTime.Parse(xNode.Attributes.GetNamedItem("StartTime").Value);

                XmlNodeList colHeaderOpNodes = xNode.SelectNodes("ColumnHeaderOptions/ColumnHeaderOption");
                if(colHeaderOpNodes !=null && colHeaderOpNodes.Count>0)
                {
                    instruction.ColumnHeaderOptions=new Dictionary<int, List<string>>();
                    foreach(XmlNode colHeaderOpNode in colHeaderOpNodes)
                    {
                        int applyOrder = int.Parse(XmlDataUtil.GetAttributeValue(colHeaderOpNode, "ApplyOrder", "1"));
                        List<string> colHeaders=new List<string>();
                        foreach(XmlNode colHeaderNode in colHeaderOpNode.ChildNodes)
                        {
                            colHeaders.Add(XmlDataUtil.GetAttributeValue(colHeaderNode, "ColumnHeader", ""));
                        }
                        instruction.ColumnHeaderOptions.Add(applyOrder, colHeaders);
                    }
                }

                instruction.FileListInputXmlFilePath = XmlDataUtil.GetAttributeValue(xNode, "FileListInputXmlFilePath", "");
                instruction.TableScanResultFile = XmlDataUtil.GetAttributeValue(xNode, "TableScanOutputFilePath", "");
                instruction.TableLayoutOutputFilePath = XmlDataUtil.GetAttributeValue(xNode, "TableLayoutOutputFilePath", "");
                instruction.LogFilePath = XmlDataUtil.GetAttributeValue(xNode, "LogFilePath", "");
                instruction.ErrFilePath = XmlDataUtil.GetAttributeValue(xNode, "ErrFilePath", "");
                instruction.ThreadPoolSize = int.Parse(XmlDataUtil.GetAttributeValue(xNode, "ThreadPoolSize", "10"));

                return instruction;
            }
            else
            {
                throw new Exception("Unable to instantiate instruction: invalid type specified in xml node");
            }
        }

        /// <summary>serialize settings to xml</summary>
        public override XmlNode Serialize(ref XmlDocument xDoc, ref XmlNode parentNode)
        {
            XmlNode xNode = XmlDataUtil.AddElement(ref xDoc, parentNode, "Instruction");
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "Type", this.GetType().FullName);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "ComponentID", this.OwnerComponentID.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "WorkerID", this.OwnerWorkerInstanceID.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "SubmitTime", this.SubmitTime.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "StartTime", this.StartTime.ToString());

            if(this.ColumnHeaderOptions !=null && ColumnHeaderOptions.Count>0)
            {
                XmlNode colHeaderOptionsNode = XmlDataUtil.AddElement(ref xDoc, xNode, "ColumnHeaderOptions");
                foreach(int applyOrder in this.ColumnHeaderOptions.Keys)
                {
                    XmlNode colHeaderOpNode = XmlDataUtil.AddElement(ref xDoc, colHeaderOptionsNode,
                                                                     "ColumnHeaderOption");
                    XmlDataUtil.UpdateAttribute(ref xDoc, colHeaderOpNode, "ApplyOrder", applyOrder.ToString());
                    List<string> colHeaders = this.ColumnHeaderOptions[applyOrder];
                    foreach(string colHeader in colHeaders)
                    {
                        XmlNode colHeaderNode = XmlDataUtil.AddElement(ref xDoc, colHeaderOpNode, "ColumnHeader");
                        XmlDataUtil.UpdateAttribute(ref xDoc, colHeaderNode, "ColumnHeader",colHeader);
                    }
                }
            }

            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "FileListInputXmlFilePath", this.FileListInputXmlFilePath);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "TableScanOutputFilePath",this.TableScanResultFile);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "TableLayoutOutputFilePath",this.TableLayoutOutputFilePath);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode,"LogFilePath",this.LogFilePath);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "ErrFilePath",this.ErrFilePath);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "ThreadPoolSize",this.ThreadPoolSize.ToString());

            return xNode;
        }
    }
}
